package com.payment.utils.date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间以某种格式输入的工具类
 *
 * @author lm
 * @date 2020/8/11 14:04
 */
@Component
public class DateUtil {

    private static Logger logger = Logger.getLogger(DateUtil.class);

    /**
     * 根据套餐类型和订购周期计算出套餐预计到期时间
     * <p>
     * 只是理论上的到期时间
     * <p>
     * 格式：yyyy-MM-dd HH:mm:ss
     *
     * @param packageType
     * @param orderPeriod
     * @return
     */
    public static String deadlineDate(String packageType, String orderPeriod) {

        Calendar calendar = Calendar.getInstance();

        if ("0".equals(packageType) || "1".equals(packageType)) {
            calendar.add(Calendar.DATE, Integer.parseInt(orderPeriod));
        } else if ("2".equals(packageType)) {
            calendar.add(Calendar.MONTH, Integer.parseInt(orderPeriod));
        } else if ("3".equals(packageType)) {
            calendar.add(Calendar.MONTH, Integer.parseInt(orderPeriod) * 3);
        } else if ("4".equals(packageType)) {
            calendar.add(Calendar.MONTH, Integer.parseInt(orderPeriod) * 6);
        } else if ("5".equals(packageType)) {
            calendar.add(Calendar.YEAR, Integer.parseInt(orderPeriod));
        }

        Date time = calendar.getTime();
        String deadline = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
        logger.info("到期时间：" + deadline);
        return deadline;
    }

    /**
     * 获取当前日期
     * <p>
     * 可以作为订购时间
     * <p>
     * 同时也是开始激活时间
     * <p>
     * 格式：yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String currentDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = sdf.format(date);
        logger.info("开始日期：" + currentDate);
        return currentDate;
    }


    public static void main(String args[]) {
        deadlineDate("0", "3");
        currentDate();
    }
}
